import { createRouter, createWebHashHistory } from 'vue-router'

export const router = createRouter({
  history: createWebHashHistory(),
  routes: [
    {
      path: '/',
      redirect: '/home'
    },
    {
      path: '/home',
      component: () => import('../views/Home/index.vue'),
      meta: {
        title: 'Apple'
      }
    },
    {
      path: '/mac',
      component: () => import('../views/Mac/index.vue'),
      meta: {
        title: 'Mac'
      }
    },
    {
      path: '/ipad',
      component: () => import('../views/iPad/index.vue'),
      meta: {
        title: 'iPad'
      }
    },
    {
      path: '/iphone',
      component: () => import('../views/iPhone/index.vue'),
      meta: {
        title: 'iPhone'
      }
    },
    {
      path: '/music',
      component: () => import('../views/Music/index.vue'),
      meta: {
        title: 'Music'
      }
    },
    {
      path: '/watch',
      component: () => import('../views/Watch/index.vue'),
      meta: {
        title: 'Watch'
      }
    },
    {
      path: '/support',
      component: () => import('../views/Support/index.vue'),
      meta: {
        title: 'Support'
      }
    }
  ]
})

// 路由守卫
router.beforeEach((to, from, next) => {
  if (to.path === from.path) {
    next(false)
  } else {
    next()
  }
})

router.afterEach((to, from) => {
  document.title = to.meta.title as string
})

export default router